﻿using RentalIService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
namespace RentalManagement.Controllers
{
    public class HouseController : Controller
    {
        public readonly IServiceHouse _IServieceHouse;
        public HouseController(IServiceHouse IServiceHouse)
        {
            _IServieceHouse = IServiceHouse;
        }

        /// <summary>
        /// 主页
        /// </summary>
        /// <returns></returns>
        // GET: House 
        [Authorize]
        public ActionResult HouseIndex()
        {
            ViewBag.role = Common.CookieHelper.ReadUser().RoleId;
            return View();
        }
       /// <summary>
       /// 房屋信息表 查询展示数据
       /// </summary>
       /// <param name="offset"></param>
       /// <param name="limit"></param>
       /// <param name="sort"></param>
       /// <param name="order"></param>
       /// <param name="detailed"></param>
       /// <param name="address"></param>
       /// <returns></returns>
        public ActionResult HouseSelect(int offset, int limit, string sort, string order, string detailed,string address)
        {
            int count = 0;
            if (string.IsNullOrWhiteSpace(sort))
            {
                sort = "Id";
            }
            var list = _IServieceHouse.ShowHouseList(offset, limit, sort, order, detailed, address, ref count);
            return Json(new { total = count, rows = list },JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 文本框地址模糊补全
        /// </summary>
        /// <returns></returns>
        public ActionResult AddressJson(string name)
        {
            var list = _IServieceHouse.ShowAddressList(name);
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        public ActionResult DetailedJson()
        {
            var list = _IServieceHouse.ShowDetailList();
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 房源子级  租客信息
        /// </summary>
        /// <param name="houseId"></param>
        /// <returns></returns>
        public ActionResult TableChild(string houseId)
        {
            string id = (string)JsonConvert.DeserializeObject(houseId);
            var list = _IServieceHouse.ShowChildTableList(id);
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 房源添加修改功能
        /// </summary>
        /// <param name="address"></param>
        /// <param name="Floore"></param>
        /// <param name="Detailed"></param>
        /// <param name="Type"></param>
        /// <param name="Remark"></param>
        /// <param name="HouseMoney"></param>
        /// <returns></returns>
        public ActionResult HouseAdd(int HouseLen, string address, string Floore, string Detailed, int Type, string Remark, int HouseMoney, string index)
        {
            bool flag = false;
            if (index != "")
            {
                flag = _IServieceHouse.UpdaHouse(index, HouseLen, address, Floore, Detailed, Type, Remark, HouseMoney);
            }
            else
            {
                flag = _IServieceHouse.AddHouse(address, HouseLen, Floore, Detailed, Type, Remark, HouseMoney);
            }
            if (flag)
            {
                return Content("OK");
            }
            else
            {
                return Content("Error");
            }
        }
        public ActionResult HouseDel(string lists)
        {
            lists = lists.Substring(0,lists.Length-1);
            bool flag = _IServieceHouse.HouseDel(lists);
            if (flag)
            {
                return Content("OK");
            }
            else
            {
                return Content("Error");
            }
        }
        public ActionResult HouseXiaJia(string text, string id)
        {
            if (_IServieceHouse.HouseXiaJia(text, id))
            {
                return Content("OK");
            }
            else
            {
                return Content("Error");
            }
        }
    }
}